package com.learn.project2.config.shiro;

import org.apache.shiro.web.session.mgt.DefaultWebSessionManager;
import org.springframework.stereotype.Component;

import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * \* Author: Valley
 * \* Date: 2022/1/10
 * \* Time: 21:49
 * \* Description:
 */
@Component
public class SessionManager extends DefaultWebSessionManager {

    private static final String ADMIN_TOKEN = "X-CskaoyanMarket-Admin-Token";
    private static final String WX_TOKEN = "X-CskaoyanMarket-Token";

    @Override
    protected Serializable getSessionId(ServletRequest request, ServletResponse response) {
        HttpServletRequest req = (HttpServletRequest)request;
        List<String> tokenList = new ArrayList<>();
        tokenList.add(req.getHeader(ADMIN_TOKEN));
        tokenList.add(req.getHeader(WX_TOKEN));
        for (String s : tokenList) {
            if (s != null && !"".equals(s)){
                return s;
            }
        }
        return super.getSessionId(request, response);
    }
}
